﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Data_Structures_and_Algorithms.linear_list
{
    public class LinearTable: ILinearList
    {
        /// <summary>
        /// 头节点
        /// </summary>
        private readonly LinearNode Data = new LinearNode();
        /// <summary>
        /// 数据量
        /// </summary>
        public int Count { get; protected set; } = 0;

        public void Display() {
            Console.WriteLine("---------------------");
            var displayData = Data.Next;
            while (displayData!=null) {
                Console.WriteLine(displayData.Data);
                displayData = displayData.Next;
            }
        }

        /// <summary>
        /// 插入节点
        /// </summary>
        /// <param name="index"></param>
        /// <param name="data"></param>
        public void Insert(int index, object data)
        {
            if (index-1 > Count) throw new Exception("超出长度！");
            var newNode = new LinearNode(data);
            // 获取index-1 节点
            var indexNode = GetNode(index-1);
            // 获取index 节点
            var indexNextNode = indexNode.Next;
            // index-1 节点指向 data
            indexNode.Next = newNode;
            // data 节点指向 index
            newNode.Next = indexNextNode;
            Count++;
        }

        /// <summary>
        /// 查找节点
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public int Find(object data)
        {
            var displayData = Data.Next;
            int result = 1;
            while (displayData != null)
            {
                if (data.Equals(displayData.Data )) return result;
                displayData = displayData.Next;
                result++;
            }
            return -1;
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="index"></param>
        public void Delete(int index) {
            if (index > Count) throw new Exception("数据不存在");
            // 获取index-1节点
            var indexPreNode = GetNode(index-1);
            if (indexPreNode == null) return;
            // 获取index+1节点
            var indexNode = indexPreNode.Next;
            if (indexNode == null) return;
            //index -1指向index+1
            indexPreNode.Next = indexNode.Next;
        }
        /// <summary>
        /// 更新节点
        /// </summary>
        /// <param name="index"></param>
        /// <param name="data"></param>
        public void Update(int index,object data) {
            var indexNode = GetNode(index);
            indexNode.Data = data;
        }
        public object FindAt(int index)
        {
            var result = GetNode(index) ?? new LinearNode();
            return result.Data;
        }
        protected LinearNode GetNode(int index)
        {
            var result = Data;
            for (int i = 0; i < index; i++)
            {
                if (result == null) return null;
                result = result.Next;
            }
            return result;
        }


    }
}
